<template>
	<view>
		<view v-if="loginType" class="ZheZhao"></view>
		<view v-if="loginType" class="LoginDiaLog">
			<view class="LoginDiaLogTitle">立即登录，即享政策精准匹配</view>
			<view class="LoginDiaLogText">我已阅读并同意<label @click="goToUserService">《用户服务协议》</label>和<label
					@click="goToPrivacyPolicy">《隐私政策》</label>并同意"政策一点通"使用所提交的信息用于快捷登录</view>
			<view class="LoginDiaLogBtnViews">
				<view class="LoginDiaLogButton" @click="changeLoginType">稍后</view>
				<view class="LoginDiaLogButton LoginDiaLogButtonBlue" @click="changeLoginSubmit">微信授权登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		wechatLogin
	} from "../../api/api.js";
	export default {
		props: {
			loginType: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				isLoading: false,
			};
		},
		methods: {
			goToPrivacyPolicy() {
				uni.navigateTo({
					url: "/pages/privacyPolicy/index",
				});
			},
			goToUserService() {
				uni.navigateTo({
					url: "/pages/userService/index",
				});
			},
			changeLoginType() {
				this.$emit("changeLoginTypeEvent");
			},
			async getUserProfileInfo() {
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						desc: "登录",
						success: (res) => resolve(res),
						fail: (err) => reject(err)
					});
				});
			},
			async getLoginCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: "weixin",
						success: (res) => resolve(res),
						fail: (err) => reject(err)
					});
				});
			},

			async changeLoginSubmit() {
				// #ifdef MP-WEIXIN
				if (this.isLoading) return;

				try {
					this.isLoading = true;

					const userInfo = await this.getUserProfileInfo();
					console.log("用户信息:", userInfo.userInfo);

					const loginRes = await this.getLoginCode();
					console.log("临时凭证:", loginRes.code);

					const res = await wechatLogin({ code: loginRes.code });
					console.log("登录结果:", res);

					if (res.code === 0) {
						this.$emit("changeLoginSubmitEvent");
						uni.setStorageSync("token", res.data.token);
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});
					} else {
						throw new Error(res.msg || '登录失败');
					}

				} catch (error) {
					console.error("登录失败:", error);
					uni.showToast({
						title: error.message || '登录失败，请重试',
						icon: 'none'
					});
				} finally {
					this.isLoading = false;
				}
				// #endif
				
				// #ifdef H5
				uni.navigateTo({
					url: '/pages/login/index'
				})
				// #endif
			},
		},
	};
</script>

<style lang="scss">
	.ZheZhao {
		overflow: hidden;
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 10002;
		background: rgba(0, 0, 0, 0.5);
	}

	.LoginDiaLog {
		position: fixed;
		left: 50%;
		margin-left: -351rpx;
		bottom: 160rpx;
		width: 702rpx;
		height: 378rpx;
		z-index: 10003;
		padding: 30rpx;
		box-sizing: border-box;
		background: url("/static/images/loginbj.png") no-repeat center / 702rpx 378rpx;

		.LoginDiaLogTitle {
			font-size: 38rpx;
			color: #202020;
			line-height: 50rpx;
			text-align: center;
		}

		.LoginDiaLogText {
			font-size: 28rpx;
			color: #4e5262;
			line-height: 42rpx;
			text-align: center;
			padding: 30rpx 0;

			label {
				color: #2f59fe;
			}
		}

		.LoginDiaLogBtnViews {
			display: flex;
			justify-content: space-between;

			.LoginDiaLogButton {
				width: 170rpx;
				height: 80rpx;
				line-height: 78rpx;
				box-sizing: border-box;
				border: 1px solid #cfd2d7;
				border-radius: 80rpx;
				text-align: center;

				&.LoginDiaLogButtonBlue {
					width: 440rpx;
					color: #ffffff;
					background: #2f59fe;
					border-color: #2f59fe;
				}
			}
		}
	}
</style>